package com.example.new5.adapter;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.new5.R;
import com.example.new5.entity.Hotsearch;

import java.util.List;
public class HotsearchAdapter extends RecyclerView.Adapter<HotsearchAdapter.HotsearchViewHolder> {
    private List<Hotsearch> hotsearchList;
    public HotsearchAdapter(List<Hotsearch> hotsearchList) {
        this.hotsearchList = hotsearchList;
    }
    @Override
    public HotsearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hotsearch, parent, false);
        return new HotsearchViewHolder(view);
    }
    @Override
    public void onBindViewHolder(HotsearchViewHolder holder, int position) {
        Hotsearch hotsearch = hotsearchList.get(position);
        holder.indexTextView.setText(String.valueOf(hotsearch.getIndex()));
        holder.titleTextView.setText(hotsearch.getTitle());
        holder.hotValueTextView.setText(hotsearch.getHotValue());
        holder.linkLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 打开系统浏览器
                String url = hotsearch.getLink();
                if (url != null) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    holder.itemView.getContext().startActivity(intent);
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return hotsearchList.size();
    }
    class HotsearchViewHolder extends RecyclerView.ViewHolder {
        TextView indexTextView,titleTextView,hotValueTextView;
        LinearLayout linkLinearLayout;
        public HotsearchViewHolder(View itemView) {
            super(itemView);
            indexTextView = itemView.findViewById(R.id.index);
            titleTextView = itemView.findViewById(R.id.title);
            hotValueTextView = itemView.findViewById(R.id.hotValue);
            linkLinearLayout = itemView.findViewById(R.id.link);
        }
    }
}